/*
 * Copyright (C) 2025 ameise <ameise.wang@gmail.com> - All Rights Reserved
 *
 * This file is part of e3time.
 *
 * e3time is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * e3time is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with e3time. If not, see <https://www.gnu.org/licenses/>.
 */

package e3time

import (
	"gitee.com/ameise84/e3ds/heap"
	"gitee.com/ameise84/e3lock"
	"gitee.com/ameise84/e3pool/go_pool"
	"time"
)

var (
	_gFastDur    time.Duration
	_gFastLocker e3lock.SpinLock
	_gHoldGen    *holderGen
)

func init() {
	_gHoldGen = &holderGen{
		holder: &holder{
			hp: heap.NewMin[uint64, *timer](),
		},
		tr: time.NewTimer(time.Hour),
	}
	_gHoldGen.runner = go_pool.NewGoRunner(_gHoldGen, "timer gen")
	_gHoldGen.timerRunner = go_pool.NewGoRunner(_gHoldGen, "timer runner")
	_gHoldGen.holder.doTr = _gHoldGen
	_ = _gHoldGen.runner.AsyncRun(_gHoldGen.loop)
}

type actuator interface {
	Reset(dur Duration, repeat ...int) (Time, bool)
}

func setFastForwardDuration(d Duration) error {
	_gFastLocker.Lock()
	defer _gFastLocker.Unlock()
	if d < _gFastDur {
		return ErrorFastForwardBefore
	}
	_gFastDur = d
	_gHoldGen.setFastForwardDuration(d)
	return nil
}
